#ifndef USERDB_H
#define USERDB_H
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
#include <QString>
#include <QDataStream>
#include <QByteArray>
#include <vector>
#include <opencv2/opencv.hpp>
class UserDB
{
public:
    static UserDB &instance();
    bool userLogin(QString id, QString password);
    bool userRegister(QString id, QString password);
    bool userUpdateInfo(QString name, QString id, QString old_password, QString new_password, QString tel, QString addr);
    QString getNameByFeature(cv::Mat feature, cv::Ptr<cv::FaceRecognizerSF> faceRecongnizer);
    bool insertFeatureByID(int id, cv::Mat& feature);
    bool UserClockin(int id);
    QSqlDatabase& getHandle();
private:
    UserDB();
    UserDB(const UserDB& other) = delete;
    UserDB& operator=(const UserDB& other) = delete;
    QSqlDatabase db;
};

template<typename T>
QByteArray serialVector(const std::vector<T>& data) {
    QByteArray byteArray;
    QDataStream stream(&byteArray, QIODevice::WriteOnly);
    stream << static_cast<quint32>(data.size());
    for (const auto & item : data) {
        stream << item;
    }
    return byteArray;
}

template<typename T>
std::vector<T> deSerialVector(const QByteArray& bytearray) {
    quint32 size;
    QDataStream stream(bytearray);
    stream.setVersion(QDataStream::Qt_5_9);
    stream >> size;
    std::vector<T> result;
    for (quint32 i = 0; i < size; ++i) {
        T item;
        stream >> item;
        result.push_back(item);
    }
    return result;
}

#endif // USERDB_H
